/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package casino;

import java.util.ArrayList;
import java.util.List;

/**
 * Class of the factory that creates outcome objects. When asked to create an
 * outcome, it will create one only if it is a new outcome, otherwise it will
 * return a reference to an already existing outcome.
 * 
 * @author  amaghfur
 * @since   1.0
 */
public class OutcomeFactory {
    
    /** for singleton pattern */
    private static OutcomeFactory instance;
    /** list of outcome created by this factory */
    private List<Outcome> outcomeList;
    
    /**
     * Class constructor.
     */
    private OutcomeFactory() {
        outcomeList = new ArrayList<>();
    }
    
    /**
     * Singleton pattern.
     * 
     * @return the only OutcomeFactory
     */
    public static synchronized OutcomeFactory getOutcomeFactory() {
        if (instance == null)
            instance = new OutcomeFactory();
        return instance;
    }
    
    /**
     * Getter for outcomeList
     */
    public List<Outcome> getOutcomeList() {
        return outcomeList;
    } 
    
    /**
     * Tells this OutcomeFactory to create a new outcome object or give a 
     * reference to one that already exists.
     * 
     * @param startName name of specified outcome
     * @param startOdds odds of specified outcome
     * @return          specified outcome
     */
    public Outcome createOutcome(String startName, int startNumerator, int startDenominator) {
        Outcome newOutcome = new Outcome(startName, startNumerator, startDenominator);
        int index = findIndexByOutcome(newOutcome, outcomeList);
        if (index == -1) {
            outcomeList.add(newOutcome);
            return newOutcome;
        }
        else {
            return outcomeList.get(index);
        }
    }
    
    /**
     * Tells this OutcomeFactory to create a new outcome object or give a 
     * reference to one that already exists. (denominator = 1)
     * 
     * @param startName name of specified outcome
     * @param startOdds odds of specified outcome
     * @return          specified outcome
     */
    public Outcome createOutcome(String startName, int startNumerator) {
        Outcome newOutcome = new Outcome(startName, startNumerator, 1);
        int index = findIndexByOutcome(newOutcome, outcomeList);
        if (index == -1) {
            outcomeList.add(newOutcome);
            return newOutcome;
        }
        else {
            return outcomeList.get(index);
        }
    }
    
    /**
     * Helper method that searches outcomeList argument for an element with
     * the queriedOutcome argument as its name attribute and returns its index.
     * 
     * @param queriedOutcome
     * @param outcomeList
     * @return 
     */
    private int findIndexByOutcome(Outcome queriedOutcome, List<Outcome> outcomeList) {
        for(int cnt=0; cnt < outcomeList.size(); cnt++) {
            if (queriedOutcome.getName().equals(outcomeList.get(cnt).getName())) {
                return cnt;
            }
        }
        return -1;
    }
        
}
